package com.yc.mybatis.test;

import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.util.List;


import org.junit.Test;

import com.yc.mybatis.bean.GoodsInfo;
import com.yc.mybatis.bean.GoodsType;
import com.yc.mybatis.core.SqlSession;
import com.yc.mybatis.core.SqlSessionFactory;
import com.yc.mybatis.core.SqlSessionFactoryBuilder;


public class AppTest2 {
	@Test
	public void test1() {
		try(InputStream is = this.getClass().getClassLoader().getResourceAsStream("mybaties-config.xml")){
			SqlSessionFactory factory =SqlSessionFactoryBuilder.build(is);
			
			SqlSession session = factory.openSession();
			
			List<GoodsInfo> list = session.selectList("GoodInfo.findAll");
		  
			list.forEach(System.out::println);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Test
	public void test2() {
		SqlSession session = null;
		try(InputStream is = this.getClass().getClassLoader().getResourceAsStream("mybaties-config.xml")){
			//构建SqlSessionFactory
			SqlSessionFactory factory =SqlSessionFactoryBuilder.build(is);
			
			session = factory.openSession();
			
			List<GoodsType> list = session.selectList("GoodType.findAll");
		  
			list.forEach(System.out::println);
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			if(session!=null) {
				//session.close();
			}
		}
	}
	
	@Test
	public void test3() {
		SqlSession session = null;
		try(InputStream is = this.getClass().getClassLoader().getResourceAsStream("mybaties-config.xml")){
			//构建SqlSessionFactory
			SqlSessionFactory factory =SqlSessionFactoryBuilder.build(is);
			
			session = factory.openSession();
			
			GoodsInfo gt = session.selectOne("GoodInfo.findByGno",1);
		  
			System.out.println(gt);
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			if(session!=null) {
				//session.close();
			}
		}
	}
	
	@Test
	public void test4() {
		SqlSession session = null;
		try(InputStream is = this.getClass().getClassLoader().getResourceAsStream("mybaties-config.xml")){
			//构建SqlSessionFactory
			SqlSessionFactory factory =SqlSessionFactoryBuilder.build(is);
			session = factory.openSession();
			
			GoodsInfo gi =new GoodsInfo(null,"龙宇giegie",null,null);
			
			int result = session.insert("GoodInfo.add",gi);
		  
			if(result>0) {
				System.out.println("成功");
				//session.commit();
			}else {
				System.out.println("失败");
			}
			
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			if(session!=null) {
				//session.close();
			}
		}
	}
	
	@Test
	public void test5() {
		SqlSession session = null;
		try(InputStream is = this.getClass().getClassLoader().getResourceAsStream("mybaties-config.xml")){
			//构建SqlSessionFactory
			SqlSessionFactory factory =SqlSessionFactoryBuilder.build(is);
			session = factory.openSession();
			
			GoodsInfo gi =new GoodsInfo(14,"龙宇",null,null);
			
			int result = session.update("GoodInfo.update",gi);
		  
			if(result>0) {
				System.out.println("成功");
				//session.commit();
			}else {
				System.out.println("失败");
			}
			
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			if(session!=null) {
				//session.close();
			}
		}
	}
}
